<template>
    <a-modal
        title=""
        :visible="true"
        width="40%"
        :footer="null"
        dialogClass="custom-modal"
        :maskClosable="false"
        @cancel="closeStopModal"
    >
        <a-form-model
            :model="stopForm"
            ref="stopForm"
            :rules="stopRules"
            :colon="false"
            class="m-form-model"
            labelAlign="left"
            layout="vertical"
        >
            <a-row :gutter="10">
                <a-col v-if="reasonType === 'stop'" :span="24" class="mb16">
                    <a-form-model-item :label="iln8[reasonType].checkLabel" prop="jsonRecord">
                        <a-checkbox-group v-model="stopForm.jsonRecord" :disabled="true">
                            <a-row>
                                <a-col :span="24" class="mb10" v-for="(checkItem, index) in stopCheckList" :key="index">
                                    <a-checkbox :value="checkItem.value" name="type" style="color: #243144">
                                        {{ index + 1 + '.' + checkItem.label }}
                                    </a-checkbox>
                                </a-col>
                            </a-row>
                        </a-checkbox-group>
                    </a-form-model-item>
                </a-col>
                <a-col :span="24" class="mb16">
                    <a-form-model-item :label="iln8[reasonType].reasonLabel" prop="reason">
                        <a-textarea
                            :disabled="true"
                            autoComplete="off"
                            :placeholder="''"
                            v-model.trim="stopForm.reason"
                            allowClear
                        />
                    </a-form-model-item>
                </a-col>
            </a-row>
        </a-form-model>
        <div class="h-end-center">
            <a-button @click="closeStopModal" class="mr20" type="primary">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
import { stopCheckList, backCheckList } from '@/views/applyManage/config.js'
import { getReason } from '@/common/api/applyManage'
import Bus from '@/common/bus.js'

export default {
    props: {
        record: {
            required: true,
            type: Object,
        },
    },
    data() {
        return {
            reasonType: 'back',
            iln8: {
                back: {
                    checkLabel: '退回理由',
                    reasonLabel: '退回原因',
                },
                stop: {
                    checkLabel: '终止理由',
                    reasonLabel: '终止原因',
                },
            },
            stopCheckList: [],
            stopForm: {
                jsonRecord: null,
                reason: null,
            },
            stopRules: {
                jsonRecord: [
                    {
                        required: true,
                        message: '请至少选择一条终止理由',
                    },
                ],
                reason: [
                    {
                        required: true,
                        message: '请输入终止理由',
                    },
                ],
            },
        }
    },
    mounted() {
        setTimeout(() => {
            this.init()
        }, 100)
    },
    methods: {
        async init() {
            Bus.$emit('loadingStateChange', true)
            let cjzt = this.record.cjzt
            if (cjzt === 3 || cjzt === 6) {
                this.stopCheckList = []
                this.reasonType = 'back'
                delete this.stopForm.jsonRecord
            }
            if (cjzt === 4 || cjzt === 7) {
                this.stopCheckList = stopCheckList
                this.reasonType = 'stop'
            }

            try {
                let params = {
                    id: this.record.id,
                    state: this.record.cjzt,
                }
                let res = await getReason(params)
                if (res.code == 200) {
                    let obj = JSON.parse(res.data)
                    this.stopForm.jsonRecord = obj.jsonRecord
                    this.stopForm.reason = obj.reason
                }
            } catch (e) {
                console.log(e)
            }
            Bus.$emit('loadingStateChange', false)
        },
        closeStopModal(type = null) {
            this.$emit('close', type)
        },
    },
}
</script>

<style lang="scss" scoped></style>
